﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace QueryGroupBuilder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        Grouper grouper;
        
        private void button_BuildGroup_Click(object sender, EventArgs e)
        {
            List<string> Characters = GetCharacterNames();
            
            grouper = new Grouper(Characters);

            grouper.SetCount(GetCountSet());

            grouper.SetLengthLimitted(GetLengthLimiteed());

            grouper.BuildList();

            List<string> list = grouper.ItemList as List<string>;

            if (NeedFilte())
            {
                list = new FlitePure().Filte(list);
            }

            box1.SetList(list);
            

        }

        private bool NeedFilte()
        {
            return checkBox1.Checked;
        }

        private int GetLengthLimiteed()
        {
            try
            {
                return int.Parse(textBox_Length.Text.Trim());
            }
            catch 
            {
                
                return 20;
            }
        }

        private int GetCountSet()
        {
            try
            {
                return int.Parse(textBox_Count.Text.Trim());
            }
            catch 
            {

                return 0;
            }
        }

        private List<string> GetCharacterNames()
        {
            List<string> names = new List<string>();

            if (checkedListBox1.CheckedItems.Count<=0)
            {
                return names;
            }

            foreach (object item in checkedListBox1.CheckedItems)
            {
                names.Add(item.ToString());
            }

            return names;

        }

        private void button_SaveAsTxt_Click(object sender, EventArgs e)
        {
            IList<string> strs= box1.GetList();
            
            string path = GetPath();

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            int piecesCount = GetPiecesCount();

            FileApp fileApp = new FileApp();

           bool IsOk=fileApp. SaveList(strs, path, piecesCount);

           if (IsOk)
           {
               MessageBox.Show("ok");
               return;
           }
           MessageBox.Show("fail");         

        }





        private int GetPiecesCount()
        {
            try
            {
                return int.Parse(textBox_PiecesCount.Text.Trim());
            }
            catch 
            {

                return 1;
            }
        }



        private string GetPath()
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                return saveFileDialog1.FileName;
            }
            return null;
        }
    }
}
